import { wxRequest } from "../../utils/httpRequest"
import { getDistance, sortBy } from "../../utils/util"
const app = getApp()

Page({
  data: {
    inputSearchValue: '',
    recommendShopList: [],
    searchHistoryList: []
  },
  searchRecommendShop() {
    wxRequest('searchRecommendShop', {}).then((res) => {
        let list = res.data;
            wx.getLocation({
                type: 'wgs84',
                success: (res)=> {
                    list.map((item) => {
                        item.distance = getDistance(item.latitude, item.longitude, res.latitude, res.longitude);
                        return item;
                    });
                    this.setData({ recommendShopList: list.sort(sortBy("distance")).reverse()});
                }
            })
    });
  },
  getHistorySearchList() {
    wxRequest('getHistorySearchList', {}).then((res) => {
        this.setData({ searchHistoryList: res.data });
    });
  },
  fieldValueChange(event) {
    this.setData({ inputSearchValue: event.detail });
  },
  doSearch() {
    wx.navigateTo({
        url: `/home_package/resultShop/resultShop?searchKeywords=${this.data.inputSearchValue}`,
    })
  },
  recordDoSearch(event) {
        this.setData({ inputSearchValue: event.currentTarget.dataset.search });
        this.doSearch();
  },
  onLoad() {
    this.searchRecommendShop();
    this.getHistorySearchList();
  }
})